package com.gitee.wsl.service

import com.gitee.wsl.service.task.AbstractTask
import com.gitee.wsl.service.task.RetryAbleTask
import com.gitee.wsl.service.task.scope.RetryAbleTaskScope
import kotlin.time.Instant
import kotlinx.datetime.LocalDateTime
import kotlin.time.ExperimentalTime

/*
import java.time.Instant
import java.time.LocalDateTime
*/

const val RECEIVER_ALARM_ID = "RECEIVER_ALARM_ID"
const val RECEIVER_ALARM_MESSAGE = "RECEIVER_ALARM_MESSAGE"

data class AlarmItem(var id:String, val time: LocalDateTime, val message: String)

interface AlarmScope : RetryAbleTaskScope {
     @OptIn(ExperimentalTime::class)
     fun snooze(data: Instant)

     fun active()

     fun deactivate()
}

enum class AlarmStatus{
    UNKNOWN,ACTIVE,DEACTIVATE,SNOOZE,WAIT,RUNNING,FINISHED,FAILED
}

interface AlarmTask: RetryAbleTask<AlarmScope, AlarmStatus>, AlarmScope {
    fun fireAlarmReceive()
}

interface AlarmServer : TaskServer<AlarmScope> {

    fun schedule(item: AlarmItem)

    fun cancel(item: AlarmItem)

    fun fireAlarmReceive(alarmItemId: String)

}

object Alarm {
    lateinit var alarmServer: AlarmServer

    fun init(alarmServer: AlarmServer){
        Alarm.alarmServer =alarmServer
    }
}


abstract class AbstractAlarmServer: AbstractTaskService<AlarmTask, AlarmScope, AlarmStatus>(),
    AlarmServer {

    override fun fireAlarmReceive(alarmItemId: String) {
       getTask(alarmItemId)?.fireAlarmReceive()
    }
}

abstract class AbstractAlarmTask: AbstractTask<AlarmScope, AlarmStatus>(AlarmStatus.UNKNOWN),
    AlarmTask {
     override fun isRunning():Boolean=currentState()== AlarmStatus.RUNNING


}

